import 'package:flutter/material.dart';
import '../../utils/G.dart';

class Score extends StatefulWidget {
  const Score({Key? key}) : super(key: key);

  @override
  _ScoreState createState() => _ScoreState();
}

class _ScoreState extends State<Score> {
  List list = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('用户积分'),
          centerTitle: true,
        ),
        body: FutureBuilder(
            future: G.api.score.list(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                var res = snapshot.data as Map;
                if (res['status'] == 200) {
                  list = res['data'];

                  return Container(
                    height: G.height,
                    child: SingleChildScrollView(
                      scrollDirection: Axis.vertical,
                      physics: const BouncingScrollPhysics(),
                      child: Column(
                        children: [
                          list.isNotEmpty
                              ? _renderList()
                              : const Center(
                                  child: Text('空空如也~'),
                                  heightFactor: 10,
                                )
                        ],
                      ),
                    ),
                  );
                } else {
                  // 报错
                  G.error('请求接口失败');
                }
              } else if (snapshot.hasError) {
                return Center(
                  child: Text("${snapshot.error}"),
                );
              }

              return G.loader();
            }));
  }

  Widget _renderList() {
    return Wrap(
      spacing: 10,
      direction: Axis.horizontal,
      alignment: WrapAlignment.start,
      children: List<Widget>.generate(list.length, (int index) {
        return Container(
            margin: const EdgeInsets.fromLTRB(10, 10, 10, 0),
            color: Colors.white,
            child: ListTile(
              title: Text(
                list[index]['mark'].toString(),
                style:
                    const TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
                overflow: TextOverflow.ellipsis,
              ),
              subtitle: Text(
                list[index]['add_time'].toString(),
                style: const TextStyle(fontSize: 14, color: Colors.grey),
              ),
              trailing: Text(
                list[index]['pm'] == 1
                    ? "+" + list[index]['number'].toString()
                    : "-" + list[index]['number'].toString(),
                style: TextStyle(
                    fontSize: 18, color: Theme.of(context).primaryColor),
                overflow: TextOverflow.ellipsis,
              ),
            ));
      }),
    );
  }
}
